package com.example.jbmsuppliers.sinopec.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.MyToast;
import com.jbm.jbmsupplier.util.Login;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 李征
 * Created by 家帮忙 on 2017/1/11.
 */

public class RegisterPhotoActivity extends OvertActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    @InjectView(R.id.gophotos)
    Button gophotos;
    @InjectView(R.id.gophoto_bings)
    Button gophotoBings;
    @InjectView(R.id.phone_res)
    EditText phoneRes;
    @InjectView(R.id.idenphoto)
    Button idenphoto;
    @InjectView(R.id.authcodes_photo)
    EditText authcodesPhoto;
    @InjectView(R.id.agreement_photos)
    CheckBox agreementPhotos;
    @InjectView(R.id.re_phone_xys)
    Button rePhoneXys;
    @InjectView(R.id.procode_photo)
    EditText procodePhoto;
    private MyCount mcount;
    public final static int PHOTO_GRAINVER_CODE = 0001;
    public final static int PHOTO_BRID = 0002;
    private DialogLoading mDialog;
    private boolean IsPitch = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_register);
        ButterKnife.inject(this);
        ExitAppUtlis.getInstance().addActivity(this);
        gophotos.setOnClickListener(this);
        rePhoneXys.setOnClickListener(this);
        agreementPhotos.setOnCheckedChangeListener(this);
    }

    //获取验证码
    public void getGainverPhoto(View v) {
        if (IsPitch)
            if (phoneRes.getText().toString().trim().equals("")) {
                phoneRes.setFocusable(true);
                MyToast.show(RegisterPhotoActivity.this, "电话号码不能为空！", Toast.LENGTH_SHORT);
            } else if (phoneRes.getText().toString().length() > 11 || phoneRes.getText().toString().length() < 11) {
                phoneRes.setFocusable(true);
                MyToast.show(RegisterPhotoActivity.this, "请检查手机号长度！", Toast.LENGTH_SHORT);
            } else {
                mcount = new MyCount(90000, 1000);
                mcount.start();
                NetWork network = new NetWork(handler);
                network.getGainVersPhoto(phoneRes.getText().toString());
                mDialog = new DialogLoading(this, "获取验证码...", false);
                mDialog.show();


            }


    }

    //下一步
    public void paymentPhoto(View v) {
        if (IsPitch)
            if (phoneRes.getText().toString().trim().equals("")) {
                phoneRes.setFocusable(true);
                MyToast.show(RegisterPhotoActivity.this, "电话号码不能为空！", Toast.LENGTH_SHORT);
            } else if (phoneRes.getText().toString().length() > 11 || phoneRes.getText().toString().length() < 11) {
                phoneRes.setFocusable(true);
                MyToast.show(RegisterPhotoActivity.this, "请检查手机号长度！", Toast.LENGTH_SHORT);
            } else if (authcodesPhoto.getText().toString().equals("")) {
                phoneRes.setFocusable(true);
                MyToast.show(RegisterPhotoActivity.this, "验证号码不能为空！", Toast.LENGTH_SHORT);
            } else {
                if (result==null){
                    MyToast.show(RegisterPhotoActivity.this, "请检查验证码是否正确！", Toast.LENGTH_SHORT);
                    return;
                }

                NetWork network = new NetWork(handler);
                network.getBridPhoto(result.login_id, authcodesPhoto.getText().toString(),procodePhoto.getText().toString());
                mDialog = new DialogLoading(this, "验证中...", false);
                mDialog.show();
            }


    }

    private Login result;
    private Login restlspwd;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case PHOTO_GRAINVER_CODE: {
                    result = (Login) msg.obj;
                    if (result != null) {
                        if (result.login_id > 1) {
                            //手机号获取验证码成功
                            if (mDialog != null)
                                mDialog.dismiss();
                        } else if (result.equals("time out")) {

                            if (mcount != null) {
                                idenphoto.setText("发送验证码");
                                mcount.cancel();
                            }
                            //手机号获取验证码超时
                            if (mDialog != null)
                                mDialog.dismiss();
                            hint(result.errorinfo);
                        } else {
                            if (mcount != null) {
                                idenphoto.setText("发送验证码");
                                mcount.cancel();
                            }
                            //手机号获取验证码失败
                            if (mDialog != null)
                                mDialog.dismiss();
                            hint(result.errorinfo);
                        }
                    }
                    break;
                }
                case PHOTO_BRID: {
                    restlspwd = (Login) msg.obj;
                    if (restlspwd.login_id > 1) {
                        //搭桥成功
                        if (mDialog != null) {
                            mDialog.dismiss();
                        }
                        if (mcount != null) {
                            mcount.cancel();
                        }
                        getPwas();
                    } else if (restlspwd.equals("time out")) {
                        //搭桥超时
                        if (mDialog != null)
                            mDialog.dismiss();
                        if (mcount != null) {
                            mcount.cancel();
                        }
                        hint(restlspwd.errorinfo);
                    } else {
                        //搭桥失败
                        if (mDialog != null)
                            mDialog.dismiss();
                        if (mcount != null) {
                            mcount.cancel();
                        }
                        hint(restlspwd.errorinfo);
                    }
                    break;
                }

            }


        }
    };

    private void getPwas() {
        if (mcount != null) {
            idenphoto.setText("发送验证码");
            mcount.cancel();
        }
        startActivity(new Intent(RegisterPhotoActivity.this, PhotoLoginPwass.class).putExtra("login_id", restlspwd.login_id).putExtra("ed_phone_sjh", phoneRes.getText().toString()));
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.re_phone_xys:
                startActivity(new Intent(RegisterPhotoActivity.this, MyAgreeManage.class));
                break;
            case R.id.gophotos:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            IsPitch = true;
            gophotoBings.setTextColor(Color.WHITE);
            idenphoto.setTextColor(Color.WHITE);
        } else {
            IsPitch = false;
            gophotoBings.setTextColor(Color.DKGRAY);
            idenphoto.setTextColor(Color.DKGRAY);
        }


    }


    /*定义一个倒计时的内部类*/
    class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            idenphoto.setText("发送验证码");

        }

        @Override
        public void onTick(long millisUntilFinished) {
            idenphoto.setText("请等待90秒(" + millisUntilFinished / 1000 + ")...");

        }
    }

}
